<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="${exam.id == null ? '创建考试' : '编辑考试'} + ' - 智能教育系统'"></title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            margin: 0;
            padding-top: 80px;
        }

        .form-container {
            max-width: 600px;
            margin: 2rem auto;
            padding: 2rem;
            background: white;
            border-radius: 20px;
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        .form-control {
            width: 100%;
            padding: 0.75rem 1rem;
            border: 2px solid #e2e8f0;
            border-radius: 10px;
            transition: all 0.3s ease;
        }

        .form-control:focus {
            border-color: #6e8efb;
            box-shadow: 0 0 0 3px rgba(110,142,251,0.2);
            outline: none;
        }

        .btn-primary {
            width: 100%;
            padding: 0.75rem;
            background: linear-gradient(135deg, #6e8efb, #a777e3);
            color: white;
            border: none;
            border-radius: 10px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(110,142,251,0.4);
        }

        .alert {
            padding: 1rem;
            border-radius: 10px;
            margin-bottom: 1rem;
            animation: slideIn 0.3s ease;
        }

        @keyframes slideIn {
            from {
                transform: translateY(-10px);
                opacity: 0;
            }
            to {
                transform: translateY(0);
                opacity: 1;
            }
        }
    </style>
</head>
<body>
    <!-- 引入导航栏 -->
    <div th:replace="fragments/navbar :: navbar"></div>

    <div class="form-container">
        <h2 class="text-2xl font-bold text-center mb-6" 
            th:text="${exam.id == null ? '创建新考试' : '编辑考试'}"></h2>
        
        <!-- 错误消息 -->
        <div th:if="${error}" class="alert bg-red-100 text-red-700" th:text="${error}"></div>

        <form th:action="${exam.id == null ? '/exam/create' : '/exam/edit/' + exam.id}" 
              method="post"
              onsubmit="return validateForm()">
            
            <div class="form-group">
                <label class="block text-gray-700 mb-2">班级</label>
                <select id="classId" name="classId" class="form-control" required>
                    <option value="">请选择班级</option>
                    <option th:each="class : ${classes}"
                            th:value="${class.id}"
                            th:text="${class.className}"
                            th:selected="${exam.classEntity != null && exam.classEntity.id == class.id}">
                    </option>
                </select>
            </div>
            
            <div class="form-group">
                <label class="block text-gray-700 mb-2">考试名称</label>
                <input type="text" 
                       id="examName"
                       name="examName"
                       th:value="${exam.examName}"
                       class="form-control" 
                       required>
            </div>
            
            <div class="form-group">
                <label class="block text-gray-700 mb-2">科目</label>
                <select id="subject" name="subject" class="form-control" required>
                    <option value="">请选择科目</option>
                    <option value="语文" th:selected="${exam.subject == '语文'}">语文</option>
                    <option value="数学" th:selected="${exam.subject == '数学'}">数学</option>
                    <option value="英语" th:selected="${exam.subject == '英语'}">英语</option>
                    <option value="物理" th:selected="${exam.subject == '物理'}">物理</option>
                    <option value="化学" th:selected="${exam.subject == '化学'}">化学</option>
                    <option value="生物" th:selected="${exam.subject == '生物'}">生物</option>
                    <option value="历史" th:selected="${exam.subject == '历史'}">历史</option>
                    <option value="地理" th:selected="${exam.subject == '地理'}">地理</option>
                    <option value="政治" th:selected="${exam.subject == '政治'}">政治</option>
                </select>
            </div>
            
            <div class="form-group">
                <label class="block text-gray-700 mb-2">考试日期</label>
                <input type="datetime-local" 
                       id="examDate"
                       name="examDate"
                       th:value="${exam.examDate}"
                       class="form-control" 
                       required>
            </div>
            
            <button type="submit" class="btn-primary">
                <span th:text="${exam.id == null ? '创建考试' : '保存修改'}"></span>
            </button>
        </form>

        <script th:inline="javascript">
            function validateForm() {
                var classId = document.getElementById('classId').value;
                var examName = document.getElementById('examName').value;
                var subject = document.getElementById('subject').value;
                var examDate = document.getElementById('examDate').value;
                
                console.log('Form data:', {
                    classId: classId,
                    examName: examName,
                    subject: subject,
                    examDate: examDate
                });
                
                if (!classId) {
                    alert('请选择班级');
                    return false;
                }
                
                if (!examName) {
                    alert('请输入考试名称');
                    return false;
                }
                
                if (!subject) {
                    alert('请选择科目');
                    return false;
                }
                
                if (!examDate) {
                    alert('请选择考试日期');
                    return false;
                }
                
                // 确保classId是数字
                if (isNaN(parseInt(classId))) {
                    alert('班级ID无效');
                    return false;
                }
                
                return true;
            }
        </script>
    </div>
</body>
</html> 